// pages/schoolList/schoolList.js
var app = getApp();
var api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    schoolList: [
      // { school_name: '武汉大学', school_address: '珞喻路广埠屯', distance_um:6000},
      // { school_name: '武汉大学', school_address: '珞喻路广埠屯', distance_um: 6000 }
    ],
    markers: [],
    kw:''
  },


  getLocation() {
    // wx.showLoading({
    //   title: '加载中...',
    // })
    wx.getLocation({
      type: 'wgs84',
      success: (res) => {
        const latitude = res.latitude
        const longitude = res.longitude
        this.setData({
          latitude,
          longitude
        })
        this.getSchoolList(1);
      },
      fail: () => {
        http.showToast('定位失败!' + '', '', 'none')
      }
    })
  },

  selectSchool(e){
   // var schoolId=e.currentTarget.dataset.id;
    var { id: schoolId, lat, lng}=e.currentTarget.dataset.item,
      item = e.currentTarget.dataset.item
    console.log(lat, lng, schoolId)
    wx.navigateTo({
      url: '/pages/course/course?schoolId=' + schoolId+'&item='+JSON.stringify(item),
    })
  },

  getSchoolList(page,search=''){
    var params={
      page,
      search,
      place: this.data.latitude + ',' + this.data.longitude
    }
    api.fetchApi('course/campus_list', params).then(res=>{
      console.log(res)
      var schoolList = this.data.schoolList;
      var schoolList1 = res.list;
      if (page == 1) {
        schoolList = res.list;
      } else {
        schoolList = [...schoolList, ...schoolList1]
      }
      this.setData({
        schoolList,
        limit: schoolList1.length
      })

      var markers = this.data.markers;
     
      schoolList.forEach((item, index) => {
        markers.push({
          iconPath: "http://qnwmfx.laigl.com/iconimg/marke.png",
          id: item.campus_is,
          title: item.campus_name + ' ' + item.address,
          latitude: item.lat,
          longitude: item.lng,
          width: 35,
          height: 35
        })
      })
      var obj = {};
      markers = markers.reduce(function (item, next) {
        obj[next.id] ? '' : obj[next.id] = true && item.push(next);
       return item;
       
      }, []);
   
      console.log(markers)
      this.setData({
        markers
      })


    })
  },

  //学校搜索
  searchShcool(e){
    console.log(e)
     var kw=e.detail.value;
    this.getSchoolList(1, kw);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getLocation();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //if (this.data.limit==10){
      var page = this.data.page || 1;
      page++;
    if (this.data.limit < 15) {
        wx.showToast({
          title: '暂无更多',
          icon: 'none'
        })
        return
      }
      this.getSchoolList(page, this.data.kw);
   // }
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})